<?php
namespace wlcat\src;
class Request {

    protected $input;
    public function __construct()
    {
        $this->initInput();
    }
    public function initInput() {
        session_start();
        $this->input = array(
            'post'      => $_POST,
            'get'       => $_GET,
            'cookie'    => $_COOKIE,
            'session'   => $_SESSION
        );
    }

    /**获取input的所有方法
     * @return array
     */
    public function getMethods() {
        return array_keys($this->input);
    }

    /**获取 get,post,cookie, session值
     * @param $key post|get|cookie|session.key
     * @param string $default
     * @return null|string
     */

    public function input($key, $default = '') {
        $key = explode('.', $key);
        if (count($key) == 2 && in_array($key[0], $this->getMethods()))
        {
            $input = $this->getInputByMethod($key[0]);
            return
                empty($key[1]) ? $input :
                    (array_key_exists($key[1], $input) ? $input[$key[1]] : $default);
        }
        elseif(count($key) == 1)
        {
            return $this->input[$key[0]];
        }
        $value = $this->getValueByKey($key[0]);
        return empty($value) ? $default : $value;
    }

    /**设置 get,post,cookie,sessioni值
     * @param $key get|post|cookie|session.key  或者设置整个 get|post|cookie|session
     * @param $value 值
     * @return bool
     */
    public function setInput($key, $value)
    {
        $key = explode('.', $key);
        if (!in_array($key[0], $this->getMethods()))
        {
            exit('设置Input需指定get|post|cookie|session方法');
            return false;
        }
        if (count($key) == 1)
        {
            $this->input[$key] = $value;
        } else {
            $this->input[$key[0]][$key[1]] = $value;
            switch ($key[0])
            {
                case 'session' :
                    $this->setSession($key[1], $value);
                    break;
                case 'cookie' :
                    $this->setCookie($key[1], $value);
                    break;
            }
        }
        return true;
    }

    public function setSession($key, $value)
    {
        $_SESSION[$key] = $value;
    }
    public function setCookie($key, $value)
    {
        $_COOKIE[$key] = $value;
    }

    //根据key获取input的get/post方法value值
    public function getValueByKey($key)
    {
        $input = $this->input;
        unset($input['session']);
        unset($input['cookie']);
        foreach ($this->input as $input)
        {
            if (array_key_exists($key, $input))
            {
                $input = null;
                return $input;
            }
        }
    }

    //根据action方法获取input
    public function getInputByMethod(string $method = '')
    {
        $method = strtolower(trim($method));
        return !empty($method) && array_key_exists($method, $this->input) ? $this->input[$method] : $this->input;
    }

    public function json($data)
    {
        header('content-type:application/json;charset=utf-8');
        $data = json_encode($data, JSON_UNESCAPED_UNICODE);
        echo $data;
        exit;
    }
}